<?php
// 本类由系统自动生成，仅供测试用途
class FillAction extends CommonAction {
	private $Fill;

	public function __construct(){
		parent::__construct();
	    $this->Fill=D('Fill');
	}

    public function index(){
        if(!empty($_POST['uname'])){
            $user=M("User")->where("username='{$_POST['uname']}'")->find();
            $where['t_fill.uid']=$user['id'];
        }
        if(!empty($_POST['num'])){
            $where['t_fill.tradeno']=htmlspecialchars($_POST["num"]);
        }
//          $where["t_fill.status"]=1;
// 		$where['type']=$_GET['type']?$where['type']=$_GET['type']:"";
		if(!empty($_GET['type'])){
			$where['t_fill.type']=intval($_GET['type']);
		}
		$where['t_fill.status']=array("in",array(0,1));
		$per_num =10;
        $page = chkNum($_GET['page']) ? $_GET['page'] : 1;
		$count = $this->Fill->where($where)->count();
        $page_num = ceil($count/$per_num);
		if($page > $page_num) $page = $page_num;
        
		$rs=$this->Fill->join(array('t_user u on u.id=t_fill.uid'))->field('t_fill.*,u.username')->where($where)->order('t_fill.ctime desc')->limit(($page-1)*$per_num.','.$per_num)->select();

		$this->assign('list',$rs);
		$this->assign('module','list');
		$this->assign('page',$page);
		$this->assign('page_num',$page_num);
		$this->assign('empty','<tr><td colspan=6>没有找到数据！</td></tr>');
		$this->display('./Tpl/Admin/Fill.html');
    }

	public function add(){
		$v = $this->Fill->find($_GET['id']);
		
		$user=M("User")->where("id='{$v['uid']}'")->find();
		$v['username']=$user['username'];
		$this->assign('id',$_GET['id']);
		$this->assign($v);
	    $this->display('./Tpl/Admin/Fill.html');
	}

	public function insert(){
	    $num = trim($_POST['num']);
        if(!chkNum($num)) {
			$this->error('请输入数量');
            exit;
        }

        $mo = new Model();
		$mo->query('set autocommit=0');
		$mo->query('lock tables t_fill write,t_user write');
		$rs = array();
		$mo->query('begin');
        $fill = $mo->table('t_fill')->find($_POST['id']);
		if($fill['status']==2){
		    $mo->query('rollback');
			$this->error('已经充值了！','/?s=Admin/Fill');
			exit;
		}

		$rs[] = $mo->table('t_fill')->save(array(
			'id'=>$_POST['id'],
			'status'=>2
		));

		$rs[] = $mo->table('t_user')->save(array(
			'id'=>$_POST['uid'],
			'rmb'=>array('exp','rmb+'.$num)
		));
		$content="充值";
		$rs[]=addFinanceLog($_POST['uid'], 2, $content, $num);
		if(chkArr($rs)){	
			$mo->query('commit');
		    $mo->query('unlock tables');
			$this->success('充值成功！','/?s=Admin/Fill');
		}else{
			$mo->query('rollback');
			$this->error('充值失败！');
		}
	}
	/**
	 * 充值完成
	 */
	public function fill_ok_list(){
	    if(!empty($_POST['uname'])){
	        $user=M("User")->where("username='{$_POST['uname']}'")->find();
	        $where['t_fill.uid']=$user['id'];
	    }
	    if(!empty($_POST['tradeno'])){
	        $where['t_fill.tradeno']=$_POST["tradeno"];
	    }
	    
	    
	    $Fill=M("Fill");
	    import('ORG.Util.Page');// 导入分页类
	    $where['t_fill.status']=2;
    	$count      = $Fill->where($where)->count();// 查询满足要求的总记录数
    	$Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数
    	$show       = $Page->show();// 分页显示输出
    	// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    	$list = $Fill->where($where)->join("t_user on t_fill.uid=t_user.id")->field("t_fill.*,t_user.username")->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
    	
    	$this->assign('list',$list);// 赋值数据集
    	$this->assign('page',$show);// 赋值分页输出
    	$this->display('./Tpl/Admin/Fill_ok_list.html');
	}
}